package org.dtrd.modules.record.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.record.entity.bean.SugarValuePhotoDetail;
import org.dtrd.modules.record.entity.po.DtrdEntRdSigninSugarValuePhoto;
import org.dtrd.modules.record.mapper.DtrdEntRdSigninSugarValuePhotoMapper;
import org.dtrd.modules.record.service.IDtrdEntRdSigninSugarValuePhotoService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


/**
 * <p>
 * 血糖记录表 服务实现类
 * </p>
 *
 * @author xiao
 * @since 2022-07-25
 */
@DS("multi-datasource1")
@Service
public class DtrdEntRdSigninSugarValuePhotoServiceImpl extends ServiceImpl<DtrdEntRdSigninSugarValuePhotoMapper, DtrdEntRdSigninSugarValuePhoto> implements IDtrdEntRdSigninSugarValuePhotoService {

    @Override
    public SugarValuePhotoDetail getSugarValuePhoto(Integer patientId, String recordDate) {
        LambdaQueryWrapper<DtrdEntRdSigninSugarValuePhoto> queryWrapper = baseQueryWrapper().eq(DtrdEntRdSigninSugarValuePhoto::getPatientId, patientId);
        if (recordDate != null) {
            queryWrapper.eq(DtrdEntRdSigninSugarValuePhoto::getRecordDate, recordDate);
        }
        queryWrapper.orderByDesc(DtrdEntRdSigninSugarValuePhoto::getRecordTime).last("limit 1");
        DtrdEntRdSigninSugarValuePhoto dtrdEntRdSigninSugarValuePhoto = getOne(queryWrapper);
        return Optional.ofNullable(dtrdEntRdSigninSugarValuePhoto)
                .map(po -> new SugarValuePhotoDetail().parseFromPo(po))
                .orElse(null);
    }

    @Override
    public boolean saveOrUpdateSugarValuePhoto(SugarValuePhotoDetail dto) {
        DtrdEntRdSigninSugarValuePhoto dtrdEntRdSigninSugarValuePhoto = new DtrdEntRdSigninSugarValuePhoto();
        if (dto.getDataId() == null) {
            dtrdEntRdSigninSugarValuePhoto = dtrdEntRdSigninSugarValuePhoto.init();
        }
        dtrdEntRdSigninSugarValuePhoto.parseFromDto(dto);
        return saveOrUpdate(dtrdEntRdSigninSugarValuePhoto);
    }

    @Override
    public List<SugarValuePhotoDetail> getSugarValuePhotoList(Integer patientId, String startDate, String endDate) {
        LambdaQueryWrapper<DtrdEntRdSigninSugarValuePhoto> wrapper = baseQueryWrapper()
                .eq(DtrdEntRdSigninSugarValuePhoto::getPatientId, patientId)
                .between(DtrdEntRdSigninSugarValuePhoto::getRecordDate, startDate, endDate);
        List<DtrdEntRdSigninSugarValuePhoto> list = list(wrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            List<SugarValuePhotoDetail> dataList = new ArrayList<>(list.size());
            list.stream().map(item -> new SugarValuePhotoDetail().parseFromPo(item)).forEach(dataList::add);
            return dataList;
        }
        return null;
    }

    private LambdaQueryWrapper<DtrdEntRdSigninSugarValuePhoto> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntRdSigninSugarValuePhoto.class)
                .eq(DtrdEntRdSigninSugarValuePhoto::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntRdSigninSugarValuePhoto> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntRdSigninSugarValuePhoto.class)
                .eq(DtrdEntRdSigninSugarValuePhoto::getIsDel, 0);
    }
}
